<!--地图相关-->
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=jp9sKArGWdHbbZ3KATccaZXhIrnlwRbN"></script>
<!--加载检索信息窗口-->
<link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.css"/>
<script type="text/javascript" src="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.js"></script>
<link rel="stylesheet" href="/css/map.css"/>
<script type="text/javascript" src="http://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js"></script>
<div style="height: 99%">
    <div id="allmap" style="overflow:hidden;zoom:1;position:relative;"></div>
</div>
<script>
    (function () {
        //创建并初始化地图对象
        var currentPoint;
        var currentRegion = '河北省邯郸市魏县';//地图显示的行政区域
        var zoomLevel = 11;//地图缩放级别
        var centerPoint = pCenterPoint ? pCenterPoint : new BMap.Point(114.954647, 36.248889);//地图中心点坐标
        var map = new BMap.Map("allmap");// 创建Map实例,设置地图允许的最小/大级别
        var overlays = [];

        var pCenterPoint = null;
        if (parent.window.currentPoint) {
            pCenterPoint = new BMap.Point(parent.window.currentPoint.lng, parent.window.currentPoint.lat);
        }
        parent.window.currentPoint = null;

        //地图相关的操作
        var mapOpts = {
            init: function (map) {
                map.centerAndZoom(centerPoint, zoomLevel);// 设置中心点坐标和地图级别。
                map.enableScrollWheelZoom();//启用地图滚轮缩放功能
                map.setMapStyle({style: "grayscale"});//设置地图主题样式
                if (pCenterPoint) {
                    var overlay = new BMap.Marker(pCenterPoint);
                    //添加标注
                    map.addOverlay(overlay);
                    overlays[0] = overlay;
                }
            },
            addMapRightMenu: function (map, rightMenuItem) {
                var menu = new BMap.ContextMenu();
                for (var i = 0; i < rightMenuItem.length; i++) {
                    menu.addItem(new BMap.MenuItem(rightMenuItem[i].text, rightMenuItem[i].callback, 100));
                }
                map.addContextMenu(menu);
            },
            createBoundary: function (map) {//显示魏县行政边界轮廓
                var bdary = new BMap.Boundary();
                bdary.get(currentRegion, function (rs) {//获取行政区域
                    //建立多边形覆盖物
                    var ply = new BMap.Polyline(rs.boundaries[0], {strokeWeight: 2, strokeColor: "#ff0000"});
                    //添加覆盖物
                    map.addOverlay(ply);
                });
            }
        };


        //地图注册右键事件
        map.addEventListener('rightclick', function (e) {
            currentPoint = e.point;//更新当前地图坐标点为鼠标右键位置
        });

        mapOpts.init(map);
        mapOpts.createBoundary(map);
        mapOpts.addMapRightMenu(map, [
            {
                text: '拾取坐标',
                callback: function () {
                    //创建标注点
                    var overlay = new BMap.Marker(currentPoint);
                    map.removeOverlay(overlays[0]);
                    //添加标注
                    map.addOverlay(overlay);
                    overlays[0] = overlay;
                    parent.window.currentPoint = currentPoint;//更新当前地图坐标点为鼠标右键位置
                }
            }
        ]);

        // 定义一个控件类,即function
        function AddressSearchControl() {
            this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
            this.defaultOffset = new BMap.Size(10, 10);
        }

        // 通过JavaScript的prototype属性继承于BMap.Control
        AddressSearchControl.prototype = new BMap.Control();

        // 自定义控件必须实现自己的initialize方法,并且将控件的DOM元素返回
        // 在本方法中创建个p元素作为控件的容器,并将其添加到地图容器中
        AddressSearchControl.prototype.initialize = function (map) {
            // 创建一个DOM元素
            var p = document.createElement("p");
            p.innerHTML = '<p id="r-result"><input type="text" id="suggestId" placeholder="搜索地址" size="20" value="百度" style="width:150px;" /></p><p id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></p>';
            // 添加DOM元素到地图中
            map.getContainer().appendChild(p);
            // 将DOM元素返回
            return p;
        };


        // 创建控件
        var addressSearchControl = new AddressSearchControl();
        addressSearchControl.defaultOffset = new BMap.Size(10, 10);
        // 添加到地图当中
        map.addControl(addressSearchControl);

        var ac = new BMap.Autocomplete({"input": "suggestId", "location": map});//建立一个自动完成的对象

        ac.addEventListener("onhighlight", function (e) {  //鼠标放在下拉列表上的事件
            var str = "";
            var _value = e.fromitem.value;
            var value = "";
            if (e.fromitem.index > -1) {
                value = _value.province + _value.city + _value.district + _value.street + _value.business;
            }
            str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

            value = "";
            if (e.toitem.index > -1) {
                _value = e.toitem.value;
                value = _value.province + _value.city + _value.district + _value.street + _value.business;
            }
            str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
            G("searchResultPanel").innerHTML = str;
        });

        var myValue;
        ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
            var _value = e.item.value;
            myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
            G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

            setPlace();
        });

        function setPlace() {
            function myFun() {
                var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                map.centerAndZoom(pp, 13);//将地图视野推近到点击位置
                var overlay = new BMap.Marker(pp);
                map.removeOverlay(overlays[0]);
                map.addOverlay(overlay);    //添加标注
                overlays[0] = overlay;//保存标注结果
            }

            var local = new BMap.LocalSearch(map, { //智能搜索
                onSearchComplete: myFun
            });
            local.search(myValue);
        }

        function G(id) {
            return document.getElementById(id);
        }

    })();


</script>